
from django.http import HttpResponse
from django.views import View
from django.shortcuts import render
import home.models as models
import article.models as article_models
import home.models as home_models


class HomeView(View):
    def get(self, request):
        data = {
            "title": "home",
            "layui_this": "layui-this",
        }
        article_query_set = article_models.Article.objects.all().order_by('-id')[:8]
        articles = []
        for i in article_query_set:
            # 通过articles的id查询comments的总数
            comments_count = article_models.Comments.objects.filter(article_id=i.id).count()
            # 给articles_query_set中的每个对象添加comment属性
            i.comment = comments_count
            articles.append(i)
        return render(request, 'content/home_content.html',locals())

    def post(self, request):
        return HttpResponse('错误')
